<style>

</style>

<template>
  <p>
    <a-space>
        <a-button type="primary" @click="handleQuery()">查询</a-button>
    </a-space>
    </p>
    <a-table 
        :dataSource="tickets" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
      </template>
      <template v-else-if="column.dataIndex === 'seatCol'">
        <span v-for="item in SEAT_COL_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatCol && item.type === record.seatType">
            {{item.desc}}
          </span>
        </span>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
    </a-table>
</template>

<script>
import { defineComponent,ref,onMounted} from 'vue';
import { notification } from 'ant-design-vue';
import axios from 'axios';

export default defineComponent({
  name: "ticket-view",
  components: {  },
  setup() {
    // 展示模态窗的标签的定义
    const visible = ref(false);
    const loading = ref(false);

    // 定义车站对象
    let ticket = ref({
        id: undefined,
        memberId:undefined,
        passengerId: undefined,
        passengerName: undefined,
        trainDate: undefined,
        trainCode: undefined,
        carriageIndex: undefined,
        seatRow:undefined,
        seatCol:undefined,
        startStation:undefined,
        startTime:undefined,
        endStation:undefined,
        endTime:undefined,
        seatType:undefined,
        createTime: undefined,
        updateTime: undefined
    });

    const tickets = ref([]);
    
    const columns = [
        {
            title: '乘客姓名',
            dataIndex: 'passengerName',
            key: 'passengerName',
        },
        {
            title: '日期',
            dataIndex: 'trainDate',
            key: 'trainDate',
        },
        {
            title: '车次编号',
            dataIndex: 'trainCode',
            key: 'trainCode',
        },
        {
            title: '箱序',
            dataIndex: 'carriageIndex',
            key: 'carriageIndex',
        },
        {
            title: '排号',
            dataIndex: 'seatRow',
            key: 'seatRow',
        },
        {
            title: '列号',
            dataIndex: 'seatCol',
            key: 'seatCol',
        },
        {
            title: '出发站',
            dataIndex: 'startStation',
            key: 'startStation',
        },
        {
            title: '出发时间',
            dataIndex: 'startTime',
            key: 'startTime',
        },
        {
            title: '到达站',
            dataIndex: 'endStation',
            key: 'endStation',
        },
        {
            title: '到站时间',
            dataIndex: 'endTime',
            key: 'endTime',
        },
        {
            title: '座位类型',
            dataIndex: 'seatType',
            key: 'seatType',
        },
    ];

    let pagination =ref({
        total: 0,
        current: 1,
        pageSize: 10,
    });
    // 车站信息列表查询的方法
    const handleQuery = e => {
        // 刷新时候不带参数的判断
        if(!e){
            e = {
                page: 1,
                limit: pagination.value.pageSize
            }
        }
        // 开启loading效果
        loading.value = true
        axios.get("/member/admin/ticket/query",
            {
                params:{
                    page: e.page,
                    limit: e.limit,
                }
            })
            .then( response => {
                // 关闭loading效果
                loading.value = false
                let respData = response.data
                let trainSeatDataList = respData.data.dataList
                 // 对车厢座位类型进行转换
                 trainSeatDataList.forEach(ticket => {
                    switch (ticket.seatType) {
                        case "1":
                        ticket.seatType = "一等座"
                            break;
                        case "2":
                        ticket.seatType = "二等座"
                            break;
                        case "3":
                        ticket.seatType = "软卧"
                            break;
                        case "4":
                        ticket.seatType = "硬卧"
                            break;
                    }
                });
                // 分页控件的设置
                let total = respData.data.total
                let currentPage = respData.data.currentPage
                let pageSize = respData.data.pageSize
                tickets.value = trainSeatDataList
                pagination.value.current = currentPage
                pagination.value.pageSize = pageSize
                pagination.value.total = total
            })
            .catch( error => {
                // 关闭loading效果
                loading.value = false
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 页码的点击事件
    const handleTableChange = (pagination) => {
        console.log("pagination:",pagination.value)
        // 调用车站列表查询方法
        handleQuery({
            page: pagination.current,
            limit: pagination.pageSize
        })
    };
    // 钩子函数，页面初始化的时候执行
    onMounted(() => {
        handleQuery({
            page: pagination.value.current,
            limit: pagination.value.pageSize
        })
    });


    return {
        visible,
        loading,
        ticket,
        tickets,
        columns,
        pagination,
        handleQuery,
        handleTableChange
    };
  },
});
</script>